﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace CoreQ
{
    public class Log
    {
        public static void WriteLog(string mes)
        {
            using (StreamWriter w = new StreamWriter(HttpContext.Current.Request.MapPath("/log.txt"), true, Encoding.UTF8))
            {
                Write(mes, w);
                // Close the writer and underlying file.
                w.Close();
            }
            // Open and read the file.
            using (StreamReader r = File.OpenText(HttpContext.Current.Request.MapPath("/log.txt")))
            {
                DumpLog(r);
            }
        }

        public static void Write(string logMessage, TextWriter w)
        {
            w.Write("Log : ");
            w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                DateTime.Now.ToLongDateString());
            w.WriteLine("Lỗi : {0}", logMessage);
            w.WriteLine("-------------------------------------------");
            w.Flush();
        }

        public static void DumpLog(StreamReader r)
        {
            // While not at the end of the file, read and write lines.
            string line;
            while ((line = r.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
            r.Close();
        }
    }
}
